'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Net
Imports System.Xml
Imports System.Windows.Forms

Public Class Netspace
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String

            'create credentials
            Dim cCache As New CredentialCache
            cCache.Add(New Uri("https://usage.netspace.net.au/usage-meter/adslusage?version=4"), "Basic", New NetworkCredential(ConfigurationData.Username, ConfigurationData.Password))

            'usage
            strHTMLData = ISPGet("https://usage.netspace.net.au/usage-meter/adslusage?version=4", CookieContainer, cCache)
            'strHTMLData = My.Computer.FileSystem.ReadAllText("C:\programming\isp\netspace\adslusage.xml", System.Text.Encoding.ASCII)

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            If strHTMLData.Contains("MyNetspace login:") = True Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            Dim doc As New XmlDocument
            doc.LoadXml(strHTMLData)

            ISPReturnData.LastUpdated = Now

            Dim NodeList As XmlNodeList
            Dim Count As Integer = 0

            Dim PeakFree As Single
            Dim OffpeakFree As Single

            Count = 0
            NodeList = doc.SelectNodes("/USAGE/TRAFFIC/DATA")
            For Each NewNode As XmlNode In NodeList
                Dim UsageValue As Single = CSng(NewNode.SelectNodes("DOWNLOADS")(0).InnerText)
                Dim TypeValue As String = NewNode.SelectNodes("TYPE")(0).InnerText
                Count += 1
                If Count <= 2 Then
                    If TypeValue = "Peak" Then
                        'peak
                        ISPReturnData.PeakDownload = UsageValue
                    Else
                        'offpeak
                        ISPReturnData.OffpeakDownload = UsageValue
                    End If
                Else
                    If TypeValue = "Peak" Then
                        'peak
                        PeakFree = UsageValue
                    Else
                        'offpeak
                        OffpeakFree = UsageValue
                    End If
                End If
            Next

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            '##########################################################

            'create graph data

            'peak
            ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))

            If ConfigurationData.OffpeakAvailable = True Then
                'offpeak
                ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak " & Format(System.Math.Floor((ISPReturnData.OffpeakDownload / Val(ConfigurationData.OffpeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.OffpeakDownload & " MB)", ISPReturnData.OffpeakDownload, CSng(Val(ConfigurationData.OffpeakUsageLimit)), ISPReturnData.OffpeakNow))
            End If

            'days elapsed
            ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

            '##########################################################

            'create stats data
            ISPReturnData.TextItemList.Add(New TextItem("Peak Left :", ISPReturnData.PeakUsageRemainingDescription))
            ISPReturnData.TextItemList.Add(New TextItem("Peak Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))

            If ConfigurationData.OffpeakAvailable = True Then
                'offpeak
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak Left :", ISPReturnData.OffpeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak Avg. Left :", ISPReturnData.OffpeakUsageRemainingPerDayDescription))
            End If

            ISPReturnData.TextItemList.Add(New TextItem("Free Peak Used :", PeakFree & " MB"))
            ISPReturnData.TextItemList.Add(New TextItem("Free Offpeak Used :", OffpeakFree & " MB"))

            If ISPReturnData.ShapedNow = False Then
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
            Else
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
            End If

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try
    End Sub

End Class
